$(document).ready(function() {

    getStoreInfo();
    getStoreAddress();

    $("div.form-submit").click(() => {
        let storeInfotmp = angular.copy(storeInfo);
        let addresstmp = angular.copy(address);
        storeInfotmp.storeName = $("#storeName").val();
        storeInfotmp.storePhone = $("#storePhone").val();
        if (!angular.equals(storeInfotmp, storeInfo)) {
            storeInfo = storeInfotmp;
            updateStoreInfo();
        }
        if (addresstmp !== null) {
            addresstmp.address = $("#address").val();
            addresstmp.addressee = $("#addressee").val();
            addresstmp.phone = $("#addressPhone").val();
           if (!angular.equals(addresstmp, address)) {
               updateStoreAddress(addresstmp)
           }
        }else {
            addStoreAddress();
        }
        location.reload();
    })

    $("div.baseinfoshopName .edit-btn").click(() => {
        $("div.baseinfoshopName>span").hide();
        $("div.baseinfoshopName>div").show();
    });

    $("div.baseinfomobile .edit-btn").click(() => {
        $("div.baseinfomobile span").hide();
        $("div.baseinfomobile div.mobile>div").show();
    });

})

let storeInfo=null;
let address=null;

function getStoreInfo() {
    $.ajax({
        type: "GET",
        url: "/api/bookStore/u/getStoreInfo",
        dataType: "text",
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                storeInfo = JSON.parse(data.data);
                $("#storeName").val(storeInfo.storeName);
                $("span.desc").text(storeInfo.storeName);
                $("#storePhone").val(storeInfo.storePhone);
                $("span.mobileText").text(storeInfo.storePhone);
            }else {
                console.log(data.msg);
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })
}

function getStoreAddress() {
    $.ajax({
        type: "GET",
        url: "/api/address/u/getStoreAddress",
        dataType: "text",
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                address = JSON.parse(data.data);
                $("#address").val(address.address);
                $("#addressee").val(address.addressee);
                $("#addressPhone").val(address.phone);
            }else {
                console.log(data.msg);
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })
}

function updateStoreAddress(addresstmp) {
    address = addresstmp;
    $.ajax({
        type: "POST",
        url: "/api/address/u/updateAddress",
        dataType: "text",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(address),
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
               alert(data.msg);
            }else {
                console.log(data.msg);
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })
}

function updateStoreInfo() {
    $.ajax({
        type: "POST",
        url: "/api/bookStore/u/updateStoreInfo",
        dataType: "text",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(storeInfo),
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                alert(data.msg);
            }else {
                console.log(data.msg);
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })
}

function addStoreAddress() {
    $.ajax({
        type: "POST",
        url: "/api/address/u/addStoreAddress",
        dataType: "text",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(storeInfo),
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                alert(data.msg);
            }else {
                console.log(data.msg);
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })
}